<div class="tableform" id="dltype-info-<{$dt_id}>"><div style="text-align:right;"><{help docid="69" type="link"}><{t}>点击查看帮助<{/t}><{/help}></div>
    <h4 class="head-title">添加/编辑快递费</h4>

    <form action="index.php?app=ome&ctl=admin_expressfee&act=saveExpressfee"  method="post" class="tableform" id="form-dly_corp">
        <div class="division">
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
            <input type="hidden" name="id" value="<{$expressfee_model.id}>">
            
            <tr>
                <th><{t}>客户：<{/t}></th>
                <td><div style="position:relative;">
                        <{input type="text"  name="shop" style="width:66px; font-family:arial;" value=$expressfee_model.shop_code}>
                   </div></td>
                <th>&nbsp;</th>
                <td>&nbsp;</td>
            </tr>   
            <tr>
                <th><{t}>生效时间：<{/t}></th>
                <td><div style="position:relative;">

            <{input type="date" vtype="date" name="effective_time" style="width:66px; font-family:arial;" value=$expressfee_model.effective_time}></div></td>
                <th>&nbsp;</th>
                <td>&nbsp;</td>
            </tr> 
            <tr>
                <th><{t}>失效时间：<{/t}></th>
                <td><div style="position:relative;">

            <{input type="date" vtype="date" name="disable_time" style="width:66px; font-family:arial;" value=$expressfee_model.disable_time}></div></td>
                <th>&nbsp;</th>
                <td>&nbsp;</td>
            </tr>  
            </table>
        </div>
        <div class="division" style="display:none;">
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <th><{t}>地区费用类型：<{/t}></th>
                    <td>
                        <div id='deliveryAreaToggle' >
                            <label><input type="radio" name="setting" value='1' ><{t}>统一设置<{/t}></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label><input type="radio" name="setting" value='0' checked><{t}>指定地区费用<{/t}></label>
                        </div>
                        <script>

                            function deliveryAreaToggle(radio){
                                var tradio=radio;
                                var tradioValue=tradio.get('value');
                                var tmap={
                                    '1':$('def_dexp'),
                                    '0':$('deliveryAreabox')
                                };
                                if(tradio.checked){
                                    tmap[tradioValue].show();
                                    tmap[tradioValue].getFormElements().set('disabled',false);
                                }else{
                                    tmap[tradioValue].hide();

                                    tmap[tradioValue].getFormElements().set('disabled',true);
                                }
                            }

                        </script>    </td>
                    <th>&nbsp;</th><td>&nbsp;</td>
                </tr>
            </table>
        </div>

        <div class="division" id='def_dexp'>
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <th><{t}>重量设置(单位g)：<{/t}></th>
                    <td>
                        <{t}>首重重量&nbsp;<{/t}><input style="width:30px;" vtype="required&&unsignedint" type="text" name="firstunit" value="<{if $dt_info.firstunit}><{$dt_info.firstunit}><{else}><{/if}>" ><{t}>&nbsp;g&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;续重重量&nbsp;<{/t}><input style="width:30px;" vtype="required&&unsignedint" type="text" name="continueunit" value="<{if $dt_info.continueunit}><{$dt_info.continueunit}><{else}><{/if}>">&nbsp;g
                    </td>
                </tr>
                <tr>
                    <th><{t}>配送费用：<{/t}></th>
                    <td>
                        <div style="line-height:30px;" class='deliveryexpbox'>
                            <div class='deliveryexp' style="<{if $dt_info.dt_useexp==1}>display:none<{/if}>">
                                首重费用 <input style="width:30px;" type='text' name='firstprice' value="<{$dt_info.firstprice}>" class="_x_ipt" vtype="required&&number" caution="该项必填且只允许填写数字金额"/>&nbsp;元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                续重费用 <input style="width:30px;" type='text' name='continueprice' value="<{$dt_info.continueprice}>" class="_x_ipt" vtype="required&&number" caution="该项必填且只允许填写数字金额"/>
                                &nbsp;元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class='lnk chgexp' onclick='chaexps(this)'><{t}>使用公式<{/t}></span>

                            </div>
                            <div class='deliveryexp' style='<{if $dt_info.dt_useexp==0}>display:none<{/if}>'>
                                配送公式 <input style="width:300px;" type='text' name='dt_expressions' value="<{if $dt_info.dt_useexp}><{$dt_info.dt_expressions}><{/if}>"  class="_x_ipt" vtype="required&&checkExp1&&checkExp2" /><{button label="验证" class="checkexp" onclick="checkExp(this);"}>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class='lnk chgexp' onclick='chaexps(this)'><{t}>取消公式<{/t}></span>
                                <input type="hidden" name="dt_useexp" value="<{$dt_info.dt_useexp}>">
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </div>

        <div class="division" id='deliveryAreabox'>
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <th><{t}>费用设置：<{/t}></th>
                    <td>
                        <div  id="deliveryArea-<{$dt_info.dt_id}>" class='deliveryArea'>
                            <ol style='list-style-type:decimal;list-style:decimal'>

                                <input type='hidden' name='delidgroup'/>
                                <script>
                                    function deleteDelivery(d){
                                        if (!confirm('删除后无法恢复，确定要删除吗？')){
                                            return;
                                        }
                                        var areaid_group=d.getElement('input[name^=area_fee_conf[areaGroupId][]]');
                                        var delidgroupHidden=$E('#deliveryArea-<{$dt_info.dt_id}> input[name=delidgroup]');
                                        var _id;
                                        areaid_group.get('name').replace(/\[(\d+)?\]/,function($0,$1){
                                            _id=$1;
                                        });
                                        if(_id){
                                            delidgroupHidden.value+=(delidgroupHidden.value!=''?",":'')+_id;
                                        }
                                        d.remove();
                                    }
                                </script>

                                <{if $expressfee_model.area_fee_conf}>

                                <{foreach from=$expressfee_model.area_fee_conf item=area key=key}>
                                <li class='division' style="line-height:30px;">
                                    <div class='deliverycity'>
                                        地区 <input style="width:300px;" type='text' name='area_fee_conf[areaGroupName][]' readonly=true required="true" value='<{$area.areaGroupName}>' class="_x_ipt" vtype="required" caution="地区不能为空" onclick="regionSelect(this);">
                                        <input type='hidden' name='area_fee_conf[areaGroupId][]' value="<{$area.areaGroupId}>"/><{img class='regionSelect' src='images/bundle/editcate.gif' style='cursor:pointer;display:none;' onclick='regionSelect(this)' alt='编辑地区' title="编辑地区"}>
                                        <a onclick='deleteDelivery($(this).getParent("li"))' >删除</a>
                                    </div>
                                 
                                    <div class='deliverycorp'>
                                          物流公司&nbsp;
                                        <input type="hidden" name="area_fee_conf[logi][]" value="<{$area.logistics}>" />
                                        <{foreach from=$logi_dly_corp item=item key=key}>
                                            <input type="checkbox" name="area_fee_conf[dly_corp][]" onclick="logicheck(this);" 
                                            value="<{$item.corp_id}>"  <{if in_array($item.corp_id,$area.logisticsIns , true) }>checked<{/if}> /><{$item.name}>&nbsp;              
                                        <{/foreach}>
                                    </div><!-- 
                                    <{if in_array($area.logisticsIns , $item.corp_id,true) }>checked<{/if}> -->
                                    <div class='deliveryexpbox'>
                                    <div class='deliveryweight'>
                                    <{t}>首重重量&nbsp;<{/t}><input style="width:30px;" vtype="required&&unsignedint" type="text" name="area_fee_conf[firstunit][]" value="<{if $area.firstunit}><{$area.firstunit}><{else}><{/if}>" ><{t}>&nbsp;g&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;续重重量&nbsp;<{/t}><input style="width:30px;" vtype="required&&unsignedint" type="text" name="area_fee_conf[continueunit][]" value="<{if $area.continueunit}><{$area.continueunit}><{else}><{/if}>">g
                                    &nbsp;
                                    </div>
                                        <div class='deliveryexp' style="<{if $area.dt_useexp==1}>display:none<{/if}>">
                                            首重费用 <input style="width:30px;" type='text' name='area_fee_conf[firstprice][]' value="<{$area.firstprice}>" <{if $area.dt_useexp=="0"}>class="_x_ipt"<{/if}> vtype="required&&number" caution="该项必填且只允许填写数字金额"/>
                                                        &nbsp;元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;续重费用 <input style="width:30px;" type='text' name='area_fee_conf[continueprice][]' value="<{$area.continueprice}>" <{if $area.dt_useexp=="0"}>class="_x_ipt"<{/if}> vtype="required&&number" caution="该项必填且只允许填写数字金额"/>

                                                        &nbsp;元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class='lnk chgexp' onclick='chaexps(this)'><{t}>使用公式<{/t}></span>
                                        </div>
                                        <div class='deliveryexp' style='<{if $area.dt_useexp==0}>display:none<{/if}>'>
                                            公式 <input style="width:300px;" type='text' name='area_fee_conf[dt_expressions][]' value="<{if $area.dt_useexp}><{$area.dt_expressions}><{/if}>" <{if $area.dt_useexp=="1"}>class="_x_ipt"<{/if}> vtype="required&&checkExp1&&checkExp2" /><{button label="验证" class="checkexp" onclick="checkExp(this);"}>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class='lnk chgexp' onclick='chaexps(this)'><{t}>取消公式<{/t}></span>
                                            <input type='hidden' name='area_fee_conf[dt_useexp][]' value='<{$area.dt_useexp}>'/>
                                        </div>
                                    </div>

                                </li>

                                <{/foreach}>

                                <{else}>
                                <li class='division' style="line-height:30px;">
                                <div></div><div></div>
                                </li>
                                <li class='division' style="line-height:30px;">
                                    <div class='deliverycity'>
                                        地区 <input style="width:300px;" type='text' name='area_fee_conf[areaGroupName][]' readonly=true required="true" value='' onclick="regionSelect(this);">
                                        <input type='hidden' name='area_fee_conf[areaGroupId][]' value="" class="_x_ipt" caution="配送地区不能为空"/>
                                        <{img class='regionSelect' src='images/bundle/editcate.gif' style='cursor:pointer;display:none;' onclick='regionSelect(this)' alt='编辑地区' title="编辑地区"}>
                                        <a onclick='deleteDelivery($(this).getParent("li"))' >删除</a>
                                    </div>
                                    <div class='deliverycorp'>
                                          物流公司&nbsp;
                                        <input type="hidden" name="area_fee_conf[logi][]"  />
                                        <{foreach from=$logi_dly_corp item=item key=key}>
                                            <input type="checkbox" name="area_fee_conf[dly_corp][]" onclick="logicheck(this);" value="<{$item.corp_id}>" /><{$item.name}>&nbsp;              
                                        <{/foreach}>
                                    </div>
                                    <div class='deliveryexpbox'>
                                    <div class='deliveryweight'>
                                    <{t}>首重重量&nbsp;<{/t}><input style="width:30px;" vtype="required&&unsignedint" type="text" name="area_fee_conf[firstunit][]" value="<{if $dt_info.firstunit}><{$dt_info.firstunit}><{else}><{/if}>" ><{t}>g&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;续重重量&nbsp;<{/t}><input style="width:30px;" vtype="required&&unsignedint" type="text" name="area_fee_conf[continueunit][]" value="<{if $dt_info.continueunit}><{$dt_info.continueunit}><{else}><{/if}>">g
                                 
                                    
                                    </div>
                                        <div class='deliveryexp'>
                                            首重费用 <input style="width:30px;" type='text' name='area_fee_conf[firstprice][]' value="" class="_x_ipt" vtype="required&&number" caution="该项必填且只允许填写数字金额"/>&nbsp;元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            续重费用 <input style="width:30px;" type='text' name='area_fee_conf[continueprice][]' value="" class="_x_ipt" vtype="required&&number" caution="该项必填且只允许填写数字金额"/>元
                                            <span class='lnk chgexp' onclick='chaexps(this)'><{t}>使用公式<{/t}></span>
                                        </div>
                                        <div class='deliveryexp' style='display:none'>
                                            配送公式 <input style="width:300px;" type='text' name='area_fee_conf[dt_expressions][]' value=""  class="_x_ipt" vtype="required&&checkExp1&&checkExp2" /><{button label="验证" class="checkexp" onclick="checkExp(this);"}>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class='lnk chgexp' onclick='chaexps(this)'><{t}>取消公式<{/t}></span>
                                            <input type='hidden' name='area_fee_conf[dt_useexp][]' value='0'/>
                                        </div>
                                    </div>
                                </li>

                                <{/if}>

                            </ol>
                            <{button label="添加地区费用" class="add-dlyarea" }>
                        </div>
                    </td>
                </tr>
            </table>
        </div>


        <div class="division">

            <div class="table-action">
                
                <{if $expressfee_model.status==0 }>
                <{button type="submit" label="保存" id="btn-dly_corp" }>
                <{/if}>
            </div>
    </form>
</div>


<script>

  
    //物流选择
    function logicheck(el)
    {
        var corp=$(el).getParent('.deliverycorp');
        var iptText=corp.getElement('input[type=hidden]');
        console.log($(el).checked);
        if($(el).checked)
        {
            if(iptText.value==''){
                corp.getElement('input[type=hidden]').value=$(el).value;     
            }else{
                corp.getElement('input[type=hidden]').value=iptText.value+","+$(el).value;     
            }
        }else{
            corp.getElement('input[type=hidden]').value=corp.getElement('input[type=hidden]').value.replace($(el).value,"");
        }
    }


    $('form-dly_corp').store('target',{
        onRequest:function(){
            $('btn-dly_corp').set('disabled', 'true');
        },
        onComplete:function(jsontext){
            var json = Json.evaluate(jsontext);
            if (typeof(json.error) != 'undefined'){
                $('btn-dly_corp').set('disabled', '');
            }else{
                $('btn-dly_corp').set('disabled', 'true');
                var fg = opener.finderGroup;
                for(fid in fg){
                    if(fid){
                        fg[fid].refresh();
                    }
                }
                window.close();
            }
        }
    });


    // $("btn-search").addEvent('click',function(e){
    //     var url='?app=desktop&act=alertpages&goto='+encodeURIComponent("index.php?app=ome&ctl=admin_shop&singleselect=1");
    //     Ex_Loader('modedialog',function() {
    //         new finderDialog(url,{params:{url:'index.php?app=ome&ctl=admin_shop&act=getShopById',name:'id',type:'radio'},handle:'shop',width:1000,height:500});
    //     });
    // });

  

    $$("#deliveryAreaToggle input[name=setting]").addEvent('click',function(){

        $$("#deliveryAreaToggle input[name=setting]").each(deliveryAreaToggle);
    }).each(deliveryAreaToggle);


    function getval(expval){
        if (eval(expval) > 0.000001){
            return 1;
        }else if (eval(expval) >-0.000001&&eval(expval)< 0.000001){
            return 1/2;
        }else{
            return 0;
        }
    }
    function getceil(expval){
        if (eval(expval) > 0){
            return Math.ceil(eval(expval)-0.000001);
        }else{
            return 0;
        }
    }


    function chaexps(el){
        var  dexps=$ES('.deliveryexp',$(el).getParent('.deliveryexpbox'));
        dexps.toggleDisplay();
        dexps.each(function(i){
            if(i.getElement('input[name^=useexp]')){
                if(!i.isDisplay()){
                    i.getElement('input[name^=useexp]').set('value',0);
                }else{
                    i.getElement('input[name^=useexp]').set('value',1);
                }
            }
            else if (i.getElement('input[name^=dt_useexp]'))
            {
                if(!i.isDisplay()){
                    i.getElement('input[name^=dt_useexp]').set('value',0);
                }else{
                    i.getElement('input[name^=dt_useexp]').set('value',1);
                }
            }else if(i.getElement('input[name^=area_fee_conf[dt_useexp]]')){
                if(!i.isDisplay()){
                    i.getElement('input[name^=area_fee_conf[dt_useexp]]').set('value',0);
                }else{
                    i.getElement('input[name^=area_fee_conf[dt_useexp]]').set('value',1);
                }
            }
        });
    }

    void function(){

        var btn_add=$E('#deliveryArea-<{$dt_info.dt_id}> .add-dlyarea');
        var getItemTemplete=function(){
            var pre_firstunit='';
            var pre_continueunit='';
            if($$('.deliveryweight').getLast()){
                pre_firstunit = $$('.deliveryweight').getLast().getElement('input[name^=area_fee_conf[firstunit]').value;
                pre_continueunit = $$('.deliveryweight').getLast().getElement('input[name^=area_fee_conf[continueunit]').value;
            }
            var btn = '<{button label="验证" class="checkexp" onclick="checkExp(this);"}>';
            var itemTemplete = "<div class='deliverycity'>";
            itemTemplete+="地区";
            itemTemplete+="<input style='width:300px;' type='text' name='area_fee_conf[areaGroupName][]' readonly=true  value='' class='_x_ipt' vtype='required' caution='地区不能为空' onclick=\"regionSelect(this);\">";
            itemTemplete+="<input type='hidden' name='area_fee_conf[areaGroupId][]' value=''/>";
            itemTemplete+="<a onclick='deleteDelivery($(this).getParent(\"li\"))' >删除</a>";
            itemTemplete+="</div>";

            itemTemplete+="<div class='deliverycorp'>";
            itemTemplete+="物流公司&nbsp;";
            itemTemplete+="<input type='hidden' name='area_fee_conf[logi][]'  />";
                                        
            <{foreach from=$logi_dly_corp item=item key=key}>
                itemTemplete+="<input type='checkbox' name='area_fee_conf[dly_corp][]'  onclick='logicheck(this);' value='<{$item.corp_id}>' /><{$item.name}>&nbsp;";
            <{/foreach}>
            itemTemplete+="</div>";

            
            itemTemplete+="<div class='deliveryexpbox'><div class='deliveryweight'><{t}>首重重量&nbsp;<{/t}><input style='width:30px;' vtype='required&&unsignedint' type='text' name='area_fee_conf[firstunit][]' value='"+pre_firstunit+"' ><{t}>&nbsp;g&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;续重重量&nbsp;<{/t}><input style='width:30px;' vtype='required&&unsignedint' type='text' name='area_fee_conf[continueunit][]' value='"+pre_continueunit+"'><{t}>&nbsp;g<{/t}>";
          

            itemTemplete+="</div><div class='deliveryexp'>";
            itemTemplete+="首重费用 <input style='width:30px;' type='text' name='area_fee_conf[firstprice][]' class='_x_ipt' vtype='required&&number' caution='该项必填且只允许填写数字金额' value=''>&nbsp;元";
            itemTemplete+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;续重费用 <input style='width:30px;' type='text' name='area_fee_conf[continueprice][]' class='_x_ipt' vtype='required&&number' caution='该项必填且只允许填写数字金额' value='' >&nbsp;元";
            itemTemplete+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class='lnk chgexp' onclick='chaexps(this)'><{t}>使用公式<{/t}></span>";
            itemTemplete+="</div>";
            itemTemplete+="<div class='deliveryexp' style='display:none'>";
            itemTemplete+="配送公式 <input style='width:300px;' type='text' name='area_fee_conf[dt_expressions][]' value='' vtype='required&&checkExp1&&checkExp2' />";
            itemTemplete+= btn +"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class='lnk chgexp' onclick='chaexps(this)'><{t}>取消公式<{/t}></span>";
            itemTemplete+="<input type='hidden' name='area_fee_conf[dt_useexp][]' value='0'/>";
            itemTemplete+="</div></div>";

            return  itemTemplete;
        };
        var list=$E('#deliveryArea-<{$dt_info.dt_id}> ol');
        btn_add.addEvent('click',function(){
        

            var newitem=new Element('li',{'class':'division','style':'line-height:30px;'}).set('html',getItemTemplete()).inject(list);
            regionSelect(newitem.getElement('.regionSelect'));
        });

        $$('#dltype-info-<{$dt_id}> .chgexp').each(chaexps);

    }();


    function regionSelect(el){
        var el=$(el).getParent('.deliverycity');
        var iptText=el.getElement('input[type=text]');
        var iptHidden=el.getElement('input[type=hidden]');
        Ex_Loader('modedialog',function(){
            new ModeDialog('index.php?app=eccommon&ctl=regions&act=showRegionTreeList&p[0]='+el.uid+'&p[1]=multi',{
                width:270,height:340,params:{iptText:iptText,iptHidden:iptHidden}
            });
        });
    }

    var checkExp=function(btn){
        btn=$(btn);
        var ipt=btn.getPrevious('input');
        var expValue=ipt.getValue();
        new Dialog('index.php?app=ome&ctl=admin_dly_corp&act=checkExp&expvalue='+encodeURIComponent(expValue),
        { modal:true,
            title:'验算配送公式'
        }
    );
    }
</script>
